close all;
clear all;
fclose all;


path1=['C:\Users\Tom�s\Desktop\Temp\'];
% path1=['C:\Users\Tom�s\Desktop\Temp\SR_MLI capacitance traces\25\'];
dir_to_search=[path1]
formatpattern = fullfile(dir_to_search, '/*.ibw');
dInfoNS = dir(formatpattern);
for i=1:numel(dInfoNS)
    nameCell{i}=dInfoNS(i).name;
end 
nameCellSort=natsortfiles(nameCell);% natural order sort file names.


%%  make a logical vector for each pattern type so I can perform operations on each group of files, regardless of how they were acquired.
for i=1:numel(dInfoNS)
    temp=IBWread([dInfoNS(i).folder '\' nameCellSort{i}]);
    time3=[1:numel(temp.y)]*temp.dx*1;
%     figure;plot(time3,temp.y)
%     title(num2str(i))
    tempMat(:,i)=temp.y-prctile(temp.y,5);
   
end

tempMatfilt=medfilt1(tempMat,30);
%remove stim artifact
a=(abs(diff(mean(tempMat(:,:),2))))>2;

tempMatfilt(a,:)=NaN;
tempMatfilt(1e5:1e5+140,:)=nan;
tempMatfilt(1.0200e+05:1.0200e+05+140,:)=nan;

time=[1:size(tempMat,1)]*temp.dx*1000; % in ms


fig2=figure(2);
fig2.Renderer='Painters';
plot(time,tempMatfilt,'color' , [0 0 0 0.2],'linewidth',1)
% xlim([ 498 510])
axis off
box off
set(gcf,'color','white')
obj=scalebar('XLen',1,'XUnit','ms','YLen',20,'YUnit','pA')
hold on
plot(time,mean(tempMatfilt,2),'color',[199 34 22]./255,'linewidth',2)

xlim([ 990 1030])

   









fclose all

time=[1:numel(ipsc.y)]*ipsc.dx*1;
figure;plot(time,medfilt1(ipsc.y,5))
hold on






time2=[1:numel(gab.y)]*gab.dx*1000;
plot(time2,medfilt1(gab.y,10))
obj=scalebar('XLen',100,'XUnit','ms','YLen',80,'YUnit','pA')





%make a mockup of IO curve.
load('IONDvsG.mat')

%Convert from frequency to number of APs (0.5 s step)
IONDSpikes=IOND.*0.25; 
IOGSpikes=IOG.*0.5;
ISteps=-30:5:30;


%% make IO figure
figure
 x=ISteps;
 y1=IONDSpikes';%
 y2=IOGSpikes'; 
% shadedErrorBar(VSteps,mean(y1),std(y1)/sqrt(size(y1,1)),'lineprops','-b','transparent',1);
s=shadedErrorBar(ISteps,mean(y1),std(y1)/sqrt(size(y1,1)),'lineprops',{'color', [0 191 255]./255});
s.mainLine.LineWidth = 2;
s.patch.FaceAlpha = 0.5;
 hold on;
 
 
 s2=shadedErrorBar(ISteps,mean(y2),std(y2)/sqrt(size(y2,1)),'lineprops',{'color', [190 30 45]./255});
s2.mainLine.LineWidth = 2;
s2.patch.FaceAlpha = 0.5;


axis tight
xlim([-5 30])
Ytick2=90;
ylim([0 Ytick2]);
set(gca, 'YTick', [0:Ytick2/3:Ytick2 ]);
yticklabels({0,'','',Ytick2})
set(gcf,'color','white');
set(gca,'color','white');
xlabel('injected current (pA)')
ylabel('action potentials')
set(gca,'FontSize', 12);
set(gcf,'Position',[100 100 300 300])




